<template>
  <div>
    <div>
      <el-input v-model="pagination.queryContent" @input="findPage" style="width: 150px" clearable
                placeholder="请输入姓名"></el-input>
      <el-button @click="download" style="margin-left: 20px">导出选中数据</el-button>

      <el-button type="primary" @click="goUpload" icon="el-icon-edit" style="width:100px;margin-left: 600px">导入数据
      </el-button>
    </div>
    <div v-loading='loading'>
      <el-table
          ref="multipleTable"
          :data="userList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChanges">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            label="真实姓名"
            align="center"
            prop="realName"
        >
        </el-table-column>
        <el-table-column
            label="登录名"
            align="center"
            prop="username"
        >
        </el-table-column>
        <el-table-column
            prop="phoneNumber"
            label="手机号"
            align="center">
        </el-table-column>
        <el-table-column
            prop="region"
            label="地址"
            align="center">
        </el-table-column>
        <el-table-column
            label="性别"
            align="center">
          <template slot-scope="scope">{{ scope.row.sex == '0' ? '男' : scope.row.sex == '1' ? '女' : '保密' }}</template>
        </el-table-column>
      </el-table>
    </div>

    <div style="padding-top: 40px">
      <el-pagination
          class="pagiantion"
          :total="total"
          @current-change="handleSelectionChange"
          :current-page="pagination.currentPage"
          :page-size="pagination.pageSize"
          layout="total, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import xslx from 'xlsx';

export default {
  name: "Excel",
  data() {
    return {
      checkList: [],
      loading: false,
      userList: [],
      total: 0,
      pagination: {
        currentPage: 1,
        pageSize: 5,
        queryContent: ""
      },
    }
  },
  mounted() {
    this.findPage();
  },
  methods: {
    download() {
      if (this.checkList.length <= 0) {
        this.$message.error("请选择导出数据哦");
        return;
      }
      let arr = this.checkList.map(item => {
        return {
          ID: item.id,
          真实姓名: item.realName,
          登录名: item.username,
          电话: item.phoneNumber
        }
      });

      let sheet = xslx.utils.json_to_sheet(arr),
          book = xslx.utils.book_new();
      xslx.utils.book_append_sheet(book, sheet, "sheet1");
      xslx.writeFile(book, `user${new Date().getTime()}.xls`);
      this.checkList = [];
    },
    goUpload() {
      this.$router.replace('/upload');
    },
    handleSelectionChanges(row) {
      this.checkList = row;
      // if (row.length > 0) {
      //   for (let i = 0; i < row.length; i++) {
      //     this.checkList.push(row[i].id);
      //   }
      // }
    },
    handleSelectionChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.findPage();
    },
    findPage() {
      this.loading = true;
      this.axios.post("/user/findPage", this.pagination).then(resp => {
        if (resp.data.statusCode == 200) {
          this.total = resp.data.data.total;
          //this.pagination.toal = resp.data.data.total;
          this.userList = resp.data.data.data;
        }
        this.loading = false;
      })
    },
  }
}
</script>

<style scoped>

</style>
